﻿using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class PlcS7200Collector : ModbusTcpDataCollector
    {
        const ushort startAddress = 80;    //起始地址
        const ushort numberOfPoints = 8; //读数据长度
        public PlcS7200Collector(DataCollectionContext dataCollectionContext) : base(dataCollectionContext)
        {
        }

        protected async override Task CollectDataInternalAsync()
        {
            ushort[] ds = ModbusMaster.ReadHoldingRegisters(CollectorConfig.SlaveAddress, startAddress, numberOfPoints);
            if (ds != null)
            {
                //DI量
                //固定防顶卡瓦状态
                SetDICoilValue(1, ds[0] == 1);
                //固定承重卡瓦状态
                SetDICoilValue(2, ds[0] == 1);
                //游动防顶卡瓦状态
                SetDICoilValue(3, ds[0] == 1);
                //游动承重卡瓦状态
                SetDICoilValue(4, ds[0] == 1);
                //管柱状态
                SetDICoilValue(5, ds[0] == 1);

                //AI量
                //告警代码
                SetAISensorValue(1, ds[4]);

                //游动防顶载荷重量:1位小数，单位：T
                SetAISensorValue(2, ds[6] / 10);

                //游动承重载荷重量:1位小数，单位：T
                SetAISensorValue(3, ds[7]/10);
            }
            else
            {

            }
        }
    }
}
